<template>
  <div class="box-bg-color">
    <div class="filter-container">
      <div class="filter-item">
        <el-input v-model="searchReq.district" placeholder="请输入地区" style="width: 200px" clearable></el-input>
      </div>
      <div class="filter-item">
        <el-button type="primary" @click="searchEvent">搜索</el-button>
      </div>
      <div class="filter-action-wrapper upload-wrap">
        <div class="filter-item"></div>
      </div>
    </div>

    <k-table
      ref="tableRef"
      v-bind="tableData"
      :callback="getAccessListApi"
      :loading="loading"
      stripe
      current-row-key="id"
    >
      <template #actions="{ row }">
        <el-button type="primary" plain size="small" @click="clickEditEvent(row.id)" v-perm="'access:see'">查看</el-button>
      </template>
    </k-table>

    <!-- 查看访问详情 -->
    <Edit v-model="showEdit" :curr-id="currId"></Edit>

  </div>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import { ElMessage, type UploadRawFile } from 'element-plus'
import type { IKTableProps } from 'k-ui'

import appConfig from '@/config/index'
import { getToken } from '@/utils/cache'
import { confirmElBox, dateStrFormat, downLoad } from '@/utils'

import {
  getAccessList,
  type QueryAccessList,
  type AccessApiResult
} from '@/api/access'

import type { ListResultData, Pagination } from '@/api/base'
import Edit from './components/Edit.vue'
import { hasPerms } from '@/utils/perm'
import { useApiLock } from '_hooks'

const tableRef = ref()
const tableData = ref<IKTableProps<AccessApiResult>>({
  mode: 'config',
  data: { list: [], total: 0 },
  auto: true,
  isPager: true,
  pageSize: 20,
  index: true,
  columns: [
    { label: '访问姓名', prop: 'userName', default: '--' },
    { label: '访问邮箱', prop: 'userEmail', default: '--' },
    { label: '访问页面', prop: 'accessPage', default: '--' },
    { label: '来源网站', prop: 'sourceSite', default: '--' },
    { label: '访问地区', prop: 'district', default: '--' },
    { label: '访问设备', prop: 'accessEquipment', default: '--' },
    {
      label: '访问时间',
      prop: 'createDate',
      formatter: (row: AccessApiResult) => dateStrFormat(row.createDate as string),
      width: 100
    }
  ]
})

/* 这里判断是否有整个操作列的权限；如果没有则不显示整列 */
const hasActionPerm = hasPerms(['access:see'])
hasActionPerm && tableData.value.columns.push({ label: '操作', prop: 'actions', slot: true, width: 240 })

const loading = ref<boolean>(false)

const searchReq = ref<QueryAccessList>({
  page: 1,
  size: 20,
  district: '',
})
// 查询表格事件
const queryReq = ref<QueryAccessList>({ page: 1, size: 20 })

const getAccessListApi = async ({ page, size }: Pagination) => {
  loading.value = true
  const res = await useApiLock(() => getAccessList({ ...queryReq.value, page, size } as QueryAccessList), 500)
  loading.value = false
  if (res?.code === 200) {
    const data = res.data as ListResultData<AccessApiResult>
    tableData.value.data = data
  } else {
    ElMessage({ message: res?.msg || '网络异常，请稍后重试', type: 'error' })
  }
}

const updateSuccess = (newPage = {}) => {
  // 在当前页 重新加载数据
  tableRef.value.refreshData(newPage)
}

const searchEvent = () => {
  queryReq.value = Object.assign({}, searchReq.value)
  updateSuccess({ page: 1, size: 10 })
}

// 用户详情查看
const showEdit = ref<boolean>(false)
const currId = ref<string | undefined>(undefined)
const clickEditEvent = (id: string) => {
  currId.value = id
  showEdit.value = true
}

</script>

<style lang="scss" scoped>
.user-account-wrap {
  display: flex;
  align-items: center;
  padding: 0 10px;
}

.upload-wrap {
  display: flex;
}
</style>
